import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class StudentDao {

    //添加学生信息
    public void add(Student stu){
        String sql = "insert into t_student(name,age) values(?,?)";
        Connection conn = null;
        Statement st = null;

        try{
            //1.初始化驱动类，在初始化的时候通过静态代码块中的java.sql.DriverManager.registerDriver(new Driver())注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/learnDesignPattern?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT","root","root");
            //3.创建语句对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1,stu.getName());
            ps.setObject(2,stu.getAge());
            //4.执行sql
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            try{
                if (st != null){
                    st.close();
                }

                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    //删除学生信息
    public void delete(int id){
        String sql = "delete from t_student where id=?";
        Connection conn = null;
        Statement st = null;

        try{
            //1.初始化驱动类，在初始化的时候通过静态代码块中的java.sql.DriverManager.registerDriver(new Driver())注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/learnDesignPattern?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT","root","root");
            //3.创建语句对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1,id);
            //4.执行sql
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            try{
                if (st != null){
                    st.close();
                }

                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    //修改学生信息
    public void update(Student stu){
        String sql = "update t_student set name =?,age=? where id=?";
        Connection conn = null;
        Statement st = null;

        try{
            //1.初始化驱动类，在初始化的时候通过静态代码块中的java.sql.DriverManager.registerDriver(new Driver())注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/learnDesignPattern?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT","root","root");
            //3.创建语句对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1,stu.getName());
            ps.setObject(2,stu.getAge());
            ps.setObject(3,stu.getId());
            //4.执行sql
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            try{
                if (st != null){
                    st.close();
                }

                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
